<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script type="text/javascript" src="../layui/layui.js"></script>
    <script src="../js/jquery-1.8.3.min.js" type="text/javascript"></script>
</head>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<form class="layui-form" id="test1" style="display:none" lay-filter="example">
    <div class="layui-form-item">
        <label class="layui-form-label">部门ID</label>
        <div class="layui-input-block">
            <input type="text" name="id" autocomplete="on" class="layui-input layui-disabled" id="id"
                   style="width:100px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">职业</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="请输入部门名称" autocomplete="on" class="layui-input" id="name1"
                   style="width:100px">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">工作</label>
        <div class="layui-input-block">
            <textarea lay-verify="required" name="remanrk" id="remark1" placeholder="请输入内容"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>
</body>
<script>
    layui.use(['table', 'form', 'layer', 'element', 'laypage'], function () {
        var table = layui.table
            , layer = layui.layer
            , element = layui.element
            , laypage = layui.laypage
            , form = layui.form;

        table.render({
            elem: '#test'
            , url: 'findjob.do'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID'}
                , {field: 'name', title: '职业'}
                , {field: 'remanrk', title: '工作'}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
            ]]
            , page: true
            , id: 'testReload'
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    console.log(data.id);
                    $.ajax({
                        url: 'delete.do?id=' + data.id,
                        type: 'delete',
                        dataType: 'json',
                        success: function (data) {
                            if (data.code == 1) {
                                alert(data.info);
                                location.reload();
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {

                layer.open({
                    type:1,
                    area: ['500px',  '500px'],
                    title: '修改信息'
                    , content: $("#test1"),
                    shade:0,
                    cancel:function (layero, index) {
                        layer.closeAll();
                    }
                });

                $.ajax({
                    type: 'get',
                    url: 'findById.do',
                    data: {id:data.id},
                    dataType: 'json',
                    success: function (data) {
                        //表单赋值
                        form.val('example', {
                            "id":data.info.id,
                            "name":data.info.name,
                            "remanrk":data.info.remanrk
                        });
                    }
                });


            }
        });

        var active = {
            reload: function () {
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        name: $("#demoReload").val()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            console.log(type);
            active[type] ? active[type].call(this) : '';
        });

        //监听提交
        form.on('submit(demo1)', function (data) {

            $.ajax({
                type: 'post',
                url: 'update.do',
                data: data.field,
                dataType: 'json',
                success: function (data) {
                    if (data.code === 1) {
                        alert(data.info);
                        location.reload();
                    }
                }
            });
            return false;
        });
    });

</script>
</html>